Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

deByteOrder.hpp

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////////////
00002 /// @file deByteOrder.hpp
00003 ///
00004 /// @brief utilities for handling byte ordering on different CPU architectures
00005 ///
00006 /// @author Assassin
00007 ///
00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
00009 /// contents of this file is subject to the Destiny3D Member License which
00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
00011 ///
00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
00014 /// for any particular purpose.
00015 ///
00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
00017 ///
00018 /// <hr>
00019 ///                                 Change History
00020 /// <hr>
00021 ///
00022 /// @date June 2003
00023 /// @author Assassin
00024 /// @remarks Creation
00025 ///
00026 ///////////////////////////////////////////////////////////////////////////////
00027 
00028 #ifndef DEBYTEORDER_HPP
00029 #define DEBYTEORDER_HPP
00030 
00031 #include "deGlobalTypes.hpp"
00032 
00033 /// reverse the byte ordering in a 16-bit value.
00034 /// used via macros: deByteOrderLE16 and deByteOrderBE16
00035 static inline u16 deSwap16(u16 x)
00036 {
00037 #ifdef DE3D_CPU_I386
00038     __asm
00039     {
00040         mov ax, x
00041         xchg ah, al
00042         mov x, ax
00043     }
00044 
00045     return x;
00046 #else
00047     return (u16)((x>>8)|(x<<8));
00048 #endif
00049 }
00050 
00051 /// reverse the byte ordering in a 32-bit value.
00052 /// used via macros: deByteOrderLE32 and deByteOrderBE32
00053 static inline u32 deSwap32(u32 x)
00054 {
00055 #ifdef DE3D_CPU_I386
00056     __asm
00057     {
00058         mov eax, x
00059         bswap eax
00060         mov x, eax
00061     }
00062 
00063     return x;
00064 #else
00065     return (x<<24) | ((x&0x0000ff00)<<8) | ((x&0x00ff0000)>>8) | (x>>24);
00066 #endif
00067 }
00068 
00069 /// reverse the byte ordering in a 64-bit value.
00070 /// used via macros: deByteOrderLE64 and deByteOrderBE64
00071 static inline u64 deSwap64(u64 x)
00072 {
00073 #ifdef DE3D_CPU_I386
00074     __asm
00075     {
00076         lea edx, x
00077         mov eax, [edx]
00078         mov ebx, [edx+4]
00079         bswap eax
00080         bswap ebx
00081         mov [edx+4], eax
00082         mov [edx], ebx
00083     }
00084 
00085     return x;
00086 #else
00087     return (deSwap32((u32)x)<<32)|(deSwap32((u32)(x>>32)));
00088 #endif
00089 }
00090 
00091 #if 0 // temporarily disable these to see if they're needed instead of the more specific ones below
00092 template <typename T>
00093 static inline T deSwap16(T val)
00094 {
00095     u16 retval = deSwap16(*(u16*)(&val));
00096     return *(T*)(&retval);
00097 }
00098 
00099 template <typename T>
00100 static inline T deSwap32(T val)
00101 {
00102     u32 retval = deSwap32(*(u32*)(&val));
00103     return *(T*)(&retval);
00104 }
00105 
00106 template <typename T>
00107 static inline T deSwap64(T val)
00108 {
00109     u64 retval = deSwap64(*(u64*)(&val));
00110     return *(T*)(&retval);
00111 }
00112 
00113 #else
00114 
00115 static inline s16 deSwap16(s16 val)
00116 {
00117     u16 retval = deSwap16(*(u16*)(&val));
00118     return *(s16*)(&retval);
00119 }
00120 static inline s32 deSwap32(s32 val)
00121 {
00122     u32 retval = deSwap32(*(u32*)(&val));
00123     return *(s32*)(&retval);
00124 }
00125 static inline s64 deSwap64(s64 val)
00126 {
00127     u64 retval = deSwap64(*(u64*)(&val));
00128     return *(s64*)(&retval);
00129 }
00130 static inline float deSwap32(float val)
00131 {
00132     u32 retval = deSwap32(*(u32*)(&val));
00133     return *(float*)(&retval);
00134 }
00135 static inline double deSwap64(double val)
00136 {
00137     u64 retval = deSwap64(*(u64*)(&val));
00138     return *(double*)(&retval);
00139 }
00140 #endif
00141 
00142 #if DE3D_CPU_LITTLE_ENDIAN
00143 #   define deByteOrderLE16(x) (x)
00144 #   define deByteOrderLE32(x) (x)
00145 #   define deByteOrderLE64(x) (x)
00146 #   define deByteOrderBE16(x) deSwap16(x)
00147 #   define deByteOrderBE32(x) deSwap32(x)
00148 #   define deByteOrderBE64(x) deSwap64(x)
00149 #elif DE3D_CPU_BIG_ENDIAN
00150 #   define deByteOrderLE16(x) deSwap16(x)
00151 #   define deByteOrderLE32(x) deSwap32(x)
00152 #   define deByteOrderLE64(x) deSwap64(x)
00153 #   define deByteOrderBE16(x) (x)
00154 #   define deByteOrderBE32(x) (x)
00155 #   define deByteOrderBE64(x) (x)
00156 #endif //DE3D_CPU_LITTLE_ENDIAN
00157 
00158 
00159 #endif //DEBYTEORDER_HPP
00160 

Generated on Mon Sep 12 19:58:24 2005 for Destiny3D by doxygen1.3-rc3